<?php 
// 20100418.01 Brian Smith
// PHP to generate graph from AOL Page Test data
$conn = mysql_connect('MYSQLSERVER', 'USERNAME', 'PASSWORD') or die ('Error connecting to mysql');
mysql_select_db(pagetest); 
$query = "SELECT URL FROM IEWPG GROUP BY URL";
$result=mysql_query($query); 
//$domain="http://www.domain.com";
$domain=$_GET['domain'];
$startdatetime=$_GET['startdatetime'];
$enddatetime=$_GET['enddatetime'];
if ($startdatetime==null || $enddatetime==null) {
    $today=date("Y-m-d");
    $startdatetime="$today 00:00:00";
    $enddatetime="$today 23:59:59";
}
$dbcolumn=$_GET['dbcolumn'];
if ($dbcolumn==null) {
    $dbcolumn="ACTIVITY_TIME";
}
$int_y_pos = -1; 
$int_y_step_small = 1; 
$sql = "SELECT ID, DATETIME, $dbcolumn FROM IEWPG WHERE URL = \"$domain\" AND DATETIME > \"$startdatetime\" AND DATETIME < \"$enddatetime\" ORDER BY DATETIME";
$sql = mysql_query($sql); 
$rownum = mysql_num_rows($sql); 
?>
<html> 
  <head> 
</script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
function display_data(domain) {  
    xmlhttp=GetXmlHttpObject(); 
    if (xmlhttp==null) { 
        alert ("Your browser does not support AJAX!"); 
        return; 
    }  
    var url="graph.php"; 
    url=url+"?domain="+domain; 
    xmlhttp.onreadystatechange=function() { 
        if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
            document.getElementById('Select_domain').innerHTML=xmlhttp.responseText;
        } 
    } 
    xmlhttp.open("GET",url,true); 
    xmlhttp.send(null); 
} 

function GetXmlHttpObject() { 
    var xmlhttp=null; 
    try { 
        // Firefox, Opera 8.0+, Safari 
        xmlhttp=new XMLHttpRequest(); 
    } 
    catch (e) { 
        // Internet Explorer 
        try { 
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
        } 
        catch (e) { 
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
    } 
    return xmlhttp; 
} 

      google.load("visualization", "1", {packages:["areachart"]});
      google.setOnLoadCallback(drawChart);

      function drawChart() { 
        var data = new google.visualization.DataTable(); 
        data.addColumn('string', 'Date'); 
<?php
echo "        data.addColumn('number', '$dbcolumn');\n"; 
echo "        data.addRows($rownum);\n";
$x=0;
while($row = mysql_fetch_assoc($sql)) { 
        $int_y_pos += $int_y_step_small; 
	$data1 = mysql_result($sql,$x,DATETIME);
	$data2 = mysql_result($sql,$x,$dbcolumn);
	$data3 = mysql_result($sql,$x,ID);
        echo "        data.setValue(" . $int_y_pos . ", 0, '" . $data3 . "\ " . $data1 . "');\n";
        echo "        data.setValue(" . $int_y_pos . ", 1, " . $data2 . ");\n";
        $x=$x+1;
}
        echo "var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));";
        echo "chart.draw(data, {width: 1024, height: 640, legend: 'bottom', title: '$domain Site Performance', titleX: 'Report Name / Date', titleY: 'Milliseconds'});";
echo "}</script></head><body>";
echo "<select onchange='window.location = \"graph.php?domain=\"+(this.value)+\"&startdatetime=$startdatetime&enddatetime=$enddatetime&dbcolumn=$dbcolumn\"'>";
echo "<option>Select_domain</option>";
#<select onchange='window.location = "graph.php?domain="+(this.value)+"&startdatetime="+(this.value)+"&enddatetime="+(this.value);'>
mysql_connect('MYSQLSERVER', 'USERNAME', 'PASSWORD') or die ('Error connecting to mysql');
mysql_select_db(pagetest);
$query = "SELECT URL FROM IEWPG WHERE DATETIME > \"$startdatetime\" AND DATETIME < \"$enddatetime\" GROUP BY URL";
$result=mysql_query($query);
while(list($domain, $name)=mysql_fetch_row($result)) { 
    echo "<option value=\"".$domain."\">".$domain."</option>"; 
}
echo "<BR>";
?> 
</select>
<?php

?>
    <div id="chart_div"></div> 
    <div id="Select_domain"></div>
  </body> 

</html> 
